/**
 * 创建测试天赋石数据脚本
 * 为测试角色添加一些天赋石用于测试
 */

const mysql = require('mysql2/promise');

// 数据库配置
const dbConfig = {
  host: '43.143.253.188',
  user: 'nextjs',
  password: 'yEAiRE2jcxTyRBy5',
  database: 'nextjs',
  charset: 'utf8mb4'
};

// 测试天赋石数据
const testTalentStones = [
  {
    character_id: 2001, // 测试角色ID
    name: "强攻石",
    level: 1,
    energy: 100,
    type: 2, // 强攻石类型
    state: 0, // 未激活
    description: "提升攻击力10%"
  },
  {
    character_id: 2001,
    name: "暴击石",
    level: 1,
    energy: 100,
    type: 5, // 暴击石类型
    state: 0, // 未激活
    description: "提高暴击威力25%"
  },
  {
    character_id: 2001,
    name: "舍命石",
    level: 1,
    energy: 100,
    type: 1, // 舍命石类型
    state: 0, // 未激活
    description: "提高致命威力5%"
  },
  {
    character_id: 2001,
    name: "强血石",
    level: 1,
    energy: 100,
    type: 20, // 强血石类型
    state: 0, // 未激活
    description: "增加生命值5%"
  },
  {
    character_id: 2001,
    name: "致命石",
    level: 1,
    energy: 100,
    type: 23, // 致命石类型
    state: 0, // 未激活
    description: "提高致命几率10%"
  }
];

async function createTestTalentStones () {
  let connection;

  try {
    console.log('🔌 连接数据库...');
    connection = await mysql.createConnection(dbConfig);
    console.log('✅ 数据库连接成功');

    // 清空现有测试数据
    console.log('🗑️ 清空现有测试天赋石数据...');
    await connection.execute('DELETE FROM talent_stone WHERE character_id = 2001');
    console.log('✅ 清空完成');

    // 插入测试天赋石数据
    console.log('📝 插入测试天赋石数据...');
    for (const stone of testTalentStones) {
      await connection.execute(
        'INSERT INTO talent_stone (character_id, name, level, energy, type, state, description) VALUES (?, ?, ?, ?, ?, ?, ?)',
        [stone.character_id, stone.name, stone.level, stone.energy, stone.type, stone.state, stone.description]
      );
    }
    console.log(`✅ 插入了${testTalentStones.length}个测试天赋石`);

    // 验证数据
    console.log('🔍 验证插入的数据...');
    const [rows] = await connection.execute(
      'SELECT * FROM talent_stone WHERE character_id = 2001 ORDER BY id'
    );

    console.log('📊 插入的天赋石数据:');
    rows.forEach((stone, index) => {
      console.log(`  ${index + 1}. ${stone.name} (等级: ${stone.level}, 元气: ${stone.energy}, 状态: ${stone.state})`);
    });

    console.log('🎉 测试天赋石数据创建完成！');

  } catch (error) {
    console.error('❌ 创建测试天赋石数据失败:', error.message);
    throw error;
  } finally {
    if (connection) {
      await connection.end();
      console.log('🔌 数据库连接已关闭');
    }
  }
}

// 运行脚本
if (require.main === module) {
  createTestTalentStones().catch((error) => {
    console.error('💥 脚本执行失败:', error.message);
    process.exit(1);
  });
}

module.exports = { createTestTalentStones };
